<?php defined("MAIN_DIR") or die("No direct access");

$add_error = $edit_error = false;
if (isset($_POST['act']) && $_POST['act'] != "")
{
	if ($_POST['act'] == "add")
	{
		$title = $info = trim(filter_var($_POST['title'],FILTER_SANITIZE_MAGIC_QUOTES));
		$cat_query = "insert into `vacancy_categories` (`title`) values ('$title')";
		if (!mysql_query($cat_query))
			$add_error = true;
	}
	elseif ($_POST['act'] == "edit")
	{
		if (isset($_POST['id']) && $_POST['id'] != "")
		{
			$title = $info = trim(filter_var($_POST['title'],FILTER_SANITIZE_MAGIC_QUOTES));
			$cat_query = "update `vacancy_categories` set `title` = '$title' where `category_id` = '$_POST[id]' limit 1";
			if (!mysql_query($cat_query))
				$edit_error = true;
		}
	}
}

require_once ADMIN.HEADFILE;
?>
<div align="left">
<h1>Список категорий</h1><?php
$cat_query = "select * from `vacancy_categories` order by `category_id`";

$limit = 10;
$cat_res = mysql_query($cat_query);
$count = mysql_num_rows($cat_res);
$page = 1;
if (isset($_GET['page']))
	$page = $_GET['page'];
$max_page = ceil($count/$limit);
if ($max_page == 0)
	$max_page++;
if ($page > $max_page)
	$page = $max_page;
elseif ($page < 1)
	$page = 1;
$start_list = (ceil($page)-1)*$limit;
$cat_query .= " limit $start_list,$limit";
$cat_res = mysql_query($cat_query);
$cat_count = mysql_num_rows($cat_res);

if ($edit_error)
	echo "<p style='color:red'>Не удалось отредактировать</p>";
if ($add_error)
	echo "<p style='color:red'>Не удалось добавить</p>";
?>
<table cellpadding="5" cellspacing="0" class='light'>
	<tr id='header'>
		<td width="30px" style="border-bottom:1px gray solid">ID</td>
		<td width="400px" style="border-bottom:1px gray solid">Заголовок</td>
		<td width="100px" style="border-bottom:1px gray solid">Действия</td>
	</tr><?php
	while ($cat_row = mysql_fetch_array($cat_res))
	{
		?><tr>
			<td><?php echo $cat_row['category_id']?></td>
			<td>
				<form method="POST">
				<input type="hidden" name="act" value="edit" />
				<input type="hidden" name="id" value="<?php echo $cat_row['category_id']?>" />
				<input type="text" style="width:300px" name='title' value='<?php echo $cat_row['title']?>' />
				<button>Изменить</button>
				</form>
			</td>
			<td>
				<a onclick='return confirm_del()' href='/admin/categories/delete/<?php echo $cat_row['category_id']?>'>Удалить</a>
			</td>
		</tr><?php
	}
	?><tr>
		<td></td>
		<td>
			<form method="POST">
			<input type="hidden" name="act" value="add" />
			<input type="text" style="width:300px" name='title' />
			<button>Добавить</button>
			</form>
		</td>
		<td></td>
	</tr>
</table>
<?php require_once APP."pageskin.php"; ?>
</div>

<?php require_once ADMIN.FOOTERFILE; ?>